package calendar;

import java.awt.* ;
import java.awt.event.* ;
import java.util.Calendar;
import java.util.Date;
import javax.swing.* ;
import javax.swing.event.* ;

/* this class provides a time panel used for start/end dates */
public class TimePanel extends JPanel
{
  private static final long serialVersionUID = 92834989L;

  CalTime rtDisplay = new CalTime();
  CalTime rt = new CalTime();

  String type;

  final JComboBox monthComboBox ;
  final SpinnerNumberModel daySpinnerModel ;
  final SpinnerNumberModel yearSpinnerModel ;
  final SpinnerNumberModel hourSpinnerModel ;
  final SpinnerNumberModel minuteSpinnerModel ;

  final int [] daysInMonth =
  {
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  } ;

  JSpinner daySpinner;
  JSpinner yearSpinner;
  JSpinner hourSpinner;
  JSpinner minuteSpinner;

  void setMaxDaysInMonth ()
  {
    Number day = daySpinnerModel.getNumber() ;
    Number year = yearSpinnerModel.getNumber() ;

    int index = monthComboBox.getSelectedIndex () ;

    Integer numDays ;
    if ( index == 1 && ( year.intValue() % 4 == 0 ) )
    {
      numDays = new Integer ( 29 ) ;
    }
    else
    {
      numDays = new Integer ( daysInMonth[index] ) ;
    }

    if ( day.intValue() > numDays.intValue() )
    {
      daySpinnerModel.setValue ( numDays ) ;
    }
    daySpinnerModel.setMaximum ( numDays ) ;
  }

  TimePanel ( String type )
  {
    JLabel label ;

    String [] monthLabels =
    {
      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    } ;

    this.type = type;

    monthComboBox = new JComboBox ( monthLabels ) ;
    monthComboBox.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;

    ActionListener actionListener = new ActionListener ()
    {
      public void actionPerformed ( ActionEvent ae )
      {
        setMaxDaysInMonth () ;
      }
    } ;

    monthComboBox.addActionListener ( actionListener ) ;

    daySpinnerModel = new SpinnerNumberModel ( 1, 1, 31, 1 ) ;
    daySpinner = new JSpinner ( daySpinnerModel ) ;
    daySpinner.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;

    yearSpinnerModel = new SpinnerNumberModel ( 2011, 2011, 2050, 1 ) ;
    yearSpinner = new JSpinner ( yearSpinnerModel ) ;
    yearSpinner.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;

    yearSpinner.setEditor ( new JSpinner.NumberEditor( yearSpinner, "#" ) ) ;

    ChangeListener changeListener = new ChangeListener ()
    {
      public void stateChanged ( ChangeEvent e )
      {
        setMaxDaysInMonth () ;
      }
    } ;

    yearSpinner.addChangeListener ( changeListener ) ;

    JPanel datePanel = new JPanel () ;

    datePanel.setLayout ( new BoxLayout( datePanel, BoxLayout.X_AXIS ) ) ;

    label = new JLabel( type ) ;
    label.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;
    datePanel.add ( label ) ;
    datePanel.add ( monthComboBox ) ;
    datePanel.add ( daySpinner ) ;
    datePanel.add ( yearSpinner ) ;

    hourSpinnerModel = new SpinnerNumberModel ( 0, 0, 23, 1 ) ;
    hourSpinner = new JSpinner ( hourSpinnerModel ) ;
    hourSpinner.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;

    minuteSpinnerModel = new SpinnerNumberModel ( 0, 0, 59, 1 ) ;
    minuteSpinner = new JSpinner ( minuteSpinnerModel ) ;
    minuteSpinner.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;

    label = new JLabel( " H: " ) ;
    label.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;
    datePanel.add ( label ) ;
    datePanel.add ( hourSpinner ) ;

    label = new JLabel( " M: " ) ;
    label.setFont ( new Font( "Dialog", Font.BOLD, 12 ) ) ;
    datePanel.add ( label ) ;
    datePanel.add ( minuteSpinner ) ;

    datePanel.setAlignmentX ( Component.LEFT_ALIGNMENT ) ;

    setLayout ( new BoxLayout( this, BoxLayout.X_AXIS ) ) ;

    Dimension d = new Dimension ( 400, 20 ) ;

    datePanel.setMinimumSize ( d ) ;
    datePanel.setMaximumSize ( d ) ;
    datePanel.setSize ( d ) ;

    add ( datePanel ) ;

    this.setEditable( true );

    /* set the date/time selection for today's date and time */
    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();
    this.setTime( now );
  }

  Date getDate ()
  {
    int month = monthComboBox.getSelectedIndex () ;

    Number num = daySpinnerModel.getNumber () ;
    int day = num.intValue () ;

    num = yearSpinnerModel.getNumber () ;
    int year = num.intValue () ;

    num = hourSpinnerModel.getNumber () ;
    int hour = num.intValue () ;

    num = minuteSpinnerModel.getNumber () ;
    int minute = num.intValue () ;

    return new Date( year - 1900, month, day, hour, minute );
  }

  void setTime ( Date date )
  {
    yearSpinnerModel.setValue( new Integer( date.getYear() + 1900 ) );
    monthComboBox.setSelectedIndex( date.getMonth() );
    daySpinnerModel.setValue( new Integer( date.getDate() ) );
    hourSpinnerModel.setValue( new Integer( date.getHours() ) );
    minuteSpinnerModel.setValue( new Integer( date.getMinutes() ) );
  }

  public void setEditable( boolean isEditable )
  {
   monthComboBox.setEnabled( isEditable );
   daySpinner.setEnabled( isEditable );
   yearSpinner.setEnabled( isEditable );
   hourSpinner.setEnabled( isEditable );
   minuteSpinner.setEnabled( isEditable );
  }
}
